package com.cedarhd.finance.msg.controller.api;

import com.cedarhd.finance.msg.common.JsonResult;
import com.cedarhd.finance.msg.common.ProcessAPI;
import com.cedarhd.finance.msg.common.enums.ResultCode;
import com.cedarhd.finance.msg.controller.Routes;
import com.cedarhd.finance.msg.domain.service.MailService;
import com.cedarhd.finance.msg.dto.MailDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lipei
 * @date 2018/3/6
 */
@Api(value = "发送电子邮件", description = "发送电子邮件")
@Controller
public class MailController {
    private Logger logger = LoggerFactory.getLogger(MailController.class);

    @Autowired
    private MailService mailService;

    /**
     * 发送邮件
     *
     * @param req
     * @return
     */
    @ApiOperation(value = "发送电子邮件", notes = "发送电子邮件", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = Routes.EMAIL_SEND, method = RequestMethod.POST)
    @ResponseBody
    public JsonResult send(HttpServletRequest request, @RequestBody MailDTO req) {
        ProcessAPI api = (result, validate) -> {
            validate.check("content",req.getContent());
            validate.check("subject",req.getSubject());
            validate.check("toAddress",req.getToAddress());
            logger.info("send params = {}",req.toString());

            Boolean sendResult = mailService.send(req);
            if(!sendResult){
                result.setMsg(ResultCode.FAILED.getMsg());
                result.setResultCode(ResultCode.FAILED.getCode());
                return;
            }
        };
        return api.process(request);

    }




}
